package com.starlink.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.starlink.common.PageQueryRequest;
import com.starlink.common.utils.AssertUtil;
import com.starlink.model.domain.ShoppingCartOrderDetailsDO;
import com.starlink.model.dtomapper.BookReadingRecordDtoMapper;
import com.starlink.model.domain.BookReadingRecordDO;
import com.starlink.mapper.BookReadingRecordMapper;
import com.starlink.model.request.BookReadingRecordRequest;
import com.starlink.model.request.save.BookReadingRecordSaveRequest;
import com.starlink.model.util.BookReadingRecordConvert;
import com.starlink.service.BookReadingRecordService;
import com.starlink.model.vo.BookReadingRecordVO;
import com.starlink.service.MallOrderDetailsService;
import com.starlink.service.ShoppingCartOrderDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.starlink.common.enums.BooleanEnum.TRUE;
import static com.starlink.common.enums.OrderTypeEnum.BOOK;
import static com.starlink.common.enums.OrderTypeEnum.CART;
import static com.starlink.utils.IDUtils.getId;
import static com.starlink.utils.PageUtils.getOrderBy;

/**
 * @author zhanghong
 */
@Service
public class BookReadingRecordServiceImpl extends ServiceImpl<BookReadingRecordMapper, BookReadingRecordDO> implements BookReadingRecordService {
    @Autowired
    private BookReadingRecordMapper bookReadingRecordMapper;
    @Autowired
    private MallOrderDetailsService orderDetailsService;
    @Autowired
    private ShoppingCartOrderDetailsService shoppingCartOrderDetailsService;

    private static final Integer MALLBOOK = 0;
    private static final Integer BAGBOOK = 1;

    @Override
    public BookReadingRecordVO getByKey(long id) {
        BookReadingRecordVO bookReadingRecordVO = BookReadingRecordDtoMapper.INSTANCE.do2VO(getById(id));
        return bookReadingRecordVO;
    }

    @Override
    public PageInfo<BookReadingRecordVO> pageQuery(Long userId, Pageable pageable, PageQueryRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), getOrderBy(pageable.getSort())).setReasonable(true);
        List<BookReadingRecordVO> list = bookReadingRecordMapper.pageQuery(userId);
        return new PageInfo<>(list);
    }

    @Override
    public List<BookReadingRecordVO> listQuery(BookReadingRecordRequest request) {
        List<BookReadingRecordDO> list = bookReadingRecordMapper.selectDOList(BookReadingRecordDtoMapper.INSTANCE.request2DO(request));
        List<BookReadingRecordVO> bookReadingRecordVOS = BookReadingRecordConvert.buildBookReadingRecordVoList(list);
        return bookReadingRecordVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(BookReadingRecordSaveRequest request) {
        BookReadingRecordRequest recordRequest = new BookReadingRecordRequest();
        Long bookId = request.getBookId();
        Long orderId = request.getOrderId();
        recordRequest.setBookId(bookId);
        recordRequest.setTitle(request.getTitle());
        recordRequest.setPicture(request.getPicture());
        recordRequest.setTextRecord(request.getTextRecord());
        recordRequest.setVoiceRecord(request.getVoiceRecord());
        recordRequest.setVideoRecord(request.getVideoRecord());
        recordRequest.setId(getId());
        save(BookReadingRecordDtoMapper.INSTANCE.request2DO(recordRequest));
        if (MALLBOOK.equals(request.getBookType())){
            //TODO:判断orderId的类型是书籍订单  还是购物车的书订单
            Integer orderCategory = request.getOrderCategory();
            if (BOOK.getType().equals(orderCategory)){
                orderDetailsService.updateRecord(bookId,orderId);
            }else if (CART.getType().equals(orderCategory)){
                shoppingCartOrderDetailsService.lambdaUpdate()
                        .set(ShoppingCartOrderDetailsDO::getRecordOrNot,TRUE.getCode())
                        .eq(ShoppingCartOrderDetailsDO::getShoppingCartOrderId,orderId)
                        .eq(ShoppingCartOrderDetailsDO::getArticlesId,bookId)
                        .update();
            }

        }
        if (BAGBOOK.equals(request.getBookType())){
            //@TODO:书包书籍记录后的操作
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByKey(BookReadingRecordRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(BookReadingRecordDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDOByIds(String ids) {
        bookReadingRecordMapper.deleteDOByIds(Convert.toStrArray(ids));
        return true;
    }

    @Override
    public PageInfo<BookReadingRecordVO> pageQueryNot(Long userId, Pageable pageable, PageQueryRequest request) {
        List<BookReadingRecordVO> list = orderDetailsService.pageQueryNot(userId,pageable,request);
        return new PageInfo<>(list);
    }
}
